function structDefs = StructParse(filename)
    hfilecontent = fileread(filename);
    indexs = strfind(hfilecontent, 'struct ');
    structNames = {};
    structInfos = {};
    cnt = 0;
    for i = 1:length(indexs)
        index0 = indexs(i);
        index1 = findLeftBrace(hfilecontent, index0);
        if isempty(index1)
            continue;
        else
            %disp(hfilecontent(index0:index1-2));
            structName = hfilecontent(index0+7:index1-2);
            %disp(structName);
            index2 = findRightBrace(hfilecontent, index1);
            if isempty(index2)
            else
                structContent = (hfilecontent(index1:index2-2));
                structInfo    = parseStruct(structContent);
                cnt = cnt + 1;
                structNames{cnt} = structName;%#ok
                structInfos{cnt} = structInfo;%#ok
            end
        end
    end
    
    [isValid, errorInfo, structDefs] = checkStructInfo(structNames, structInfos);
    if ~isValid
        error(errorInfo);
    end
end

function st = strNotInCell(str, Names)
    st = true;
    for i = 1:length(Names)
        if strcmp(str, Names{i})
            st = false;
            return;
        end
    end
end

function [isValid, errorInfo, structDefs] = checkStructInfo(structNames, structInfos)
    structDefs = {};
    isValid    = true;
    errorInfo    = '';
    for i = 1:length(structInfos)
        info = structInfos{i};
        for idx = 1:length(info)
            type = info{idx}.type;
            if info{idx}.isStruct && strNotInCell(type, structNames)
                isValid = false;    
                errorInfo = [type, ' is not defined'];
            end
        end
        structDef.structName = structNames{i};
        structDef.signalInfo = info;
        structDefs{i} = structDef;%#ok
    end
end

function structItem = parseStructItem(strLine)
    strs = strsplit(strLine);
    structItem.type = '';
    structItem.signal = '';
    structItem.isStruct = false;
    structItem.dimension = 1;
    typeStr   = strs{end-1};
    signalStr = strs{end}(1:end-1);
    structItem.type   = typeStr;
    if strNotInCell(structItem.type, {'uint8_T','real32_T','int32_T','int16_T'})
        structItem.isStruct = true;
    end
    [s1,s2] = regexp(signalStr, '\[([\d]*)\]','start','tokens');
    if isempty(s1)||isempty(s2)
        structItem.signal = signalStr;
    else
        structItem.signal    =  signalStr(1:s1(1)-1);
        structItem.dimension = str2num(s2{1}{1});%#ok
    end
end

function structInfo    = parseStruct(structContent)
    strs = strsplit(structContent, newline);
    structInfo = {};
    cnt = 0;
    for i = 1:length(strs)
        str = strs{i};
        if length(str) > 1
            structItem = parseStructItem(strtrim(str));
            if ~isempty(structItem.type)
                cnt = cnt + 1;
                structInfo{cnt} = structItem;%#ok
            end
        end
    end
end

function index1 = findLeftBrace(hfilecontent, index0)
    str = hfilecontent(index0 + (0:50));
    index = find(str == '{',1);
    if ~isempty(index)
        index1 = index + index0;
    else
        index1 = [];
    end
end

function index2 = findRightBrace(hfilecontent, index1)
    str = hfilecontent(index1:end);
    index = find(str == '}',1);
    if ~isempty(index)
        index2 = index + index1;
    else
        index2 = [];
    end
end